/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.io.File;
import java.sql.Connection;

import onpu_dms_xls.API.API;
import onpu_dms_xls.exceptions.MyException;
import onpu_dms_xls.kernel.common.DB.DBConnect;
import onpu_dms_xls.kernel.common.logAndDebug.Debug;

/**
 *
 * @author snbm
 */
public class Main {
    private static String WORK_PATH = "C:\\temp\\onpu_dms\\dms_files\\Dropbox\\temp";
    private static String HOME_PATH = "C:\\temp";

    private String CURRENT_PATH = HOME_PATH;

    private static String WORK_DB_PASS = "two*three=Six";
    private static String HOME_DB_PASS = "1111";

    private String CURRENT_DB_PASS = HOME_DB_PASS;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        new Main();
    }

    public Main() {
        Connection con = null;
        try {
            DBConnect dbc = new DBConnect("jdbc:postgresql:postgresNew", "postgres", CURRENT_DB_PASS);
            con = dbc.getConnection();
        } catch (MyException e) {
            System.out.println(e);
            return;
        }


        // make_Ітогова_відомість(con);

        make_PDF_extract(con);


    }

    void make_PDF_extract(Connection con) {


        File xmlTemplate = new File(CURRENT_PATH+"\\templates\\extractPDFDataTemplate.xml");
        File xlsTemplate = new File(CURRENT_PATH+"\\templates\\extractPDFTemplate(LIKE XLS).xml");

        try {
            API.getInstance().loadTemplates(
                    con,
                    xlsTemplate,
                    xmlTemplate,
                    666,
                    1);
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace(System.out);
            return;
        }

        File getFileResult = null;

        try {
            //getFileResult = API.XLSFromDB(
            getFileResult = API.getInstance().pdfFromDB(
                    "<fromDBInputParameters>"
                    + "<constantParameters>"
                    + "<outputFolder>"+CURRENT_PATH+"</outputFolder>"
                    //+ "<debugMode>FULL_PRINT</debugMode>" -------------------------!!!!!!!!!!!!!!!
                    + "<output_file_name>extract</output_file_name>"
                    + "<documentClassId>666</documentClassId>"
                    + "<temporaryFolder>"+CURRENT_PATH+"\\q</temporaryFolder>"
                    + "</constantParameters>"
                    + "<zeroLevelParameters>"
                    + "<param name=\"student_id\">182270</param>"
                    //+ "<param name=\"student_id\">261186</param>"
                    + "</zeroLevelParameters> "
                    + "</fromDBInputParameters>", con);
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace(System.out);
            return;
        }


        System.out.println("Result file absolute path: " + getFileResult.getAbsolutePath());
        System.out.println("Result file name: " + getFileResult.getName());

        try{
           // Runtime.getRuntime().exec("C:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe " + CURRENT_PATH+"\\extract.pdf");
        } catch (Exception e){}
    }

    void make_Ітогова_відомість(Connection con) {

        File xmlTemplate = new File("c:\\temp\\templates\\Ітогова_відомость.xml");
        File xlsTemplate = new File("c:\\temp\\templates\\Ітогова_відомость.xls");

        try {
            API.getInstance().loadTemplates(
                    con,
                    xlsTemplate,
                    xmlTemplate,
                    Debug.FULL_PRINT,
                    109,
                    1);
        } catch (MyException e) {
            System.out.println(e);
            return;
        } catch (Exception e) {
            System.out.println(e);
            return;
        }


//        File inFile = new File("c:\\temp\\111.xls");
//
//
//        try {
//            API.XLS2DB(
//                    "<fromDBInputParameters>"
//                    + "<constantParameters>"
//                    + "<debugMode>FULL_PRINT</debugMode>"
//                    + "<documentClassId>109</documentClassId>"
//                    + "<temporaryFolder>C:\\temp\\q</temporaryFolder>"
//                    + "</constantParameters>"
//                    + "</fromDBInputParameters>", con, inFile);
//        } catch (MyException e) {
//            System.out.println(e);
//            return;
//        } catch (Exception e) {
//            System.out.println(e);
//            return;
//        }



        File getFileResult = null;

        try {
            getFileResult = API.getInstance().XLSFromDB(
                    "<fromDBInputParameters>"
                    + "<constantParameters>"
                    + "<outputFolder>c:\\temp</outputFolder>"
                    + "<debugMode>FULL_PRINT</debugMode>"
                    + "<output_file_name>файл_відомості_123</output_file_name>"
                    + "<documentClassId>109</documentClassId>"
                    + "<temporaryFolder>c:\\temp\\q</temporaryFolder>"
                    + "</constantParameters>"
                    + "<zeroLevelParameters>"
                    + "<param name=\"detailed_plan_id\">657</param>"
                    + "</zeroLevelParameters> "
                    + "</fromDBInputParameters>", con);
        } catch (MyException e) {
            System.out.println(e);
            return;
        } catch (Exception e) {
            System.out.println(e);
            return;
        }


        System.out.println("Result file absolute path: " + getFileResult.getAbsolutePath());
        System.out.println("Result file name: " + getFileResult.getName());

    }
}
